<?php
 
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class SocialinsuranceController extends Controller
{
    //===============显示列表=============
    public function getIndex(Request $request){

     if($request['name']){
            //搜索分页
            $res = DB::table('socialinsurance')
            ->join('person','person.id','=','socialinsurance.Personid')
            ->where('PersonName','like','%'.$request['name'].'%')
            ->select('socialinsurance.*','person.PersonName')
            ->paginate(1);
          
            
        }else{
            //所有分页      
            $res = DB::table('socialinsurance')
            ->join('person','person.id','=','socialinsurance.PersonID') 
             ->select('socialinsurance.*','person.PersonName')
            ->paginate(1);
            
           
            }              
        $info = $request -> all();    
        return view('Personnel/Socialinsurance/index',['res'=>$res,'info'=>$info]);
}

    //=============添加========
    public function getAdd(){
         $res = DB::table('person')
        ->select('id','PersonName')
        ->get();
        //dd($res);
    	//解析模板
    	return view('Personnel/Socialinsurance/add',['res'=>$res]);
    }
    //======执行添加====
    public function postInsert(Request $request){
    	$date = $request ->all();
        //dd($date);
        //随机token
        $date['_token']=str_random(50);
    	//dd($date); 
        //添加sql语句
        $res = DB::table('socialinsurance')->insert($date);
       if($res){
            //成功跳转至列表页
            return redirect('/Personnel/Socialinsurance/index')->with('success','添加成功');
        }else{
           return back()->with('error','添加失败');
        }                                                                                     
    }
    //================编辑=======
    public function getEdit($SocialInsuranceID){

             $info = DB::table('Socialinsurance')
            ->join('person','person.id','=','socialinsurance.PersonID')
            ->where('socialinsurance.socialinsuranceID',$SocialInsuranceID)
            ->select('socialinsurance.*','person.PersonName')
            ->get();
        //解析模板显示编辑的页面
            return view('Personnel/Socialinsurance.edit',['info'=>$info,'SocialInsuranceID'=>$SocialInsuranceID]);
    }
    //================执行编辑
    public function postUpdate(Request $request){
        $info = $request ->all();
        $SocialInsuranceID = $info['SocialInsuranceID'];
        //dd($info);
        DB::table('socialinsurance')->where('SocialInsuranceID',$SocialInsuranceID)->update($info);
        return redirect('Personnel/Socialinsurance/index')->with('修改成功');
    }   
   
    //=============================删除===========================
    public function getDel($id){
        //执行删除
        //dd($id);
        DB::table('socialinsurance')->where('socialinsuranceID',$id)->delete();
        //解析模板显示列表的页面
        return redirect('Personnel/Socialinsurance/index')->with('删除成功');
    }



}
?>